<template>
    <div class="SHZY-common-layout">
        <div class="SHZY-common-layout-center">
            <el-row class="SHZY-common-search-box" :gutter="16">
                <el-form @submit.native.prevent="">
                    <el-col :span="4">
                        <el-form-item label="报检单号">
                            <el-input v-model='query.inspectionCode' placeholder='报检单号' @keyup.enter.native="search"
                                clearable required :style='{ "width": "100%" }'>
                            </el-input>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="供应商">
                            <el-select v-model="query.supplier" placeholder="供应商" @keyup.enter.native="search" clearable>
                                <el-option v-for="(item, index) in supplierOptions" :key="index" :label="item.name"
                                    :value="item.code" />
                            </el-select>
                        </el-form-item>
                    </el-col>

                    <el-col :span="4">
                        <el-form-item label="采购订单号">
                            <el-input v-model="query.orderNo" placeholder="采购订单号" @keyup.enter.native="search" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="物料">
                            <SHZY-select :list="materialOptions" v-model:bindValue="query.materialCode"
                                @keyup.enter.native="search" label="name" value="code" placeholder="物料" isConcat
                                concatSymbol=" / ">
                            </SHZY-select>
                        </el-form-item>
                    </el-col>
                    <template v-if="showAll">
                        <el-col :span="4">
                            <el-form-item label="批次号">
                                <el-input v-model='query.batchNumber' placeholder='请输入' @keyup.enter.native="search"
                                    clearable required :style='{ "width": "100%" }'>
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item label="单据状态">
                                <el-select v-model="query.status" placeholder="状态" @keyup.enter.native="search" clearable>
                                    <el-option v-for="(item, index) in statusOptions" :key="index" :label="item.name"
                                        :value="item.code" />
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </template>
                    <el-col :span="6">
                        <el-form-item>
                            <el-button type="primary" icon="el-icon-search" @click="search()">查询</el-button>
                            <el-button icon="el-icon-refresh-right" @click="reset()">重置</el-button>
                            <el-button type="text" icon="el-icon-arrow-down" @click="showAll = true"
                                v-if="!showAll">展开</el-button>
                            <el-button type="text" icon="el-icon-arrow-up" @click="showAll = false" v-else>收起</el-button>
                        </el-form-item>
                    </el-col>
                </el-form>
            </el-row>
            <div class="SHZY-common-layout-main SHZY-flex-main">
                <div class="SHZY-common-head">
                    <div>
                        <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle()">新增</el-button>
                        <el-button type="primary" @click="handleBatchConfirm()">确认</el-button>
                        <el-button type="primary" @click="handleCombineBatch()">合批</el-button>
                        <el-button type="text" icon="el-icon-delete" @click="handleBatchRemoveDel()">批量删除</el-button>
                        <el-button type="text" icon="el-icon-download" @click="exportData()">导出</el-button>
                    </div>
                    <div class="SHZY-common-head-right">
                        <el-tooltip effect="dark" content="刷新" placement="top">
                            <el-link icon="icon-ym icon-ym-Refresh SHZY-common-head-icon" :underline="false"
                                @click="reset()" />
                        </el-tooltip>
                    </div>
                </div>
                <SHZY-table v-loading="listLoading" :data="list" @sort-change='sortChange' has-c
                    @selection-change='handleSelectionChange' custom-column>
                    <el-table-column prop="overdue" label="标记" width="70" align="center">
                        <template slot="header" slot-scope="scope">
                            <el-dropdown>
                                <span class="el-dropdown-link">
                                    标记 <i class="el-icon-question"></i>
                                </span>
                                <el-dropdown-menu slot="dropdown">
                                    <el-dropdown-item command="right">
                                        <i class="ym-custom ym-custom-checkbox-blank-circle"
                                            style="color:#ff625c;font-size:14px;">逾期 </i>
                                    </el-dropdown-item>
                                    <el-dropdown-item command="bottom">
                                        <i class="ym-custom ym-custom-checkbox-blank-circle"
                                            style="color:#f9a646;font-size:14px;">即将逾期 </i>
                                    </el-dropdown-item>
                                    <el-dropdown-item command="refresh">
                                        <i class="ym-custom ym-custom-checkbox-blank-circle"
                                            style="color:#6dcc51;font-size:14px;">正常</i>
                                    </el-dropdown-item>
                                </el-dropdown-menu>
                            </el-dropdown>
                        </template>
                        <template slot-scope="scope">
                            <i class="ym-custom ym-custom-checkbox-blank-circle" style="color:#ff625c;font-size:16px;"
                                v-if="scope.row.overdue == 2"></i>
                            <i class="ym-custom ym-custom-checkbox-blank-circle" style="color:#f9a646;font-size:16px;"
                                v-else-if="scope.row.overdue == 1"></i>
                            <i class="ym-custom ym-custom-checkbox-blank-circle" style="color:#6dcc51;font-size:16px;"
                                v-else></i>
                        </template>
                    </el-table-column>
                    <el-table-column label="报检单号" prop="inspectionCode" min-width="130" :show-overflow-tooltip="true"
                        align="left" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="状态" prop="statusDesc" min-width="70" :show-overflow-tooltip="true" align="left"
                        sortable='custom'>
                        <template slot-scope="scope">
                            <el-tag v-if="scope.row.statusDesc == '未确认'" type="info">未确认</el-tag>
                            <el-tag v-if="scope.row.statusDesc == '已提交'">已提交</el-tag>
                            <el-tag v-if="scope.row.statusDesc == '已确认'" type="success">已确认</el-tag>
                            <el-tag v-if="scope.row.statusDesc == '未知状态'" type="warning">未知状态</el-tag>
                        </template>
                    </el-table-column>
                    <el-table-column label="物料编码" prop="materialCode" min-width="100" :show-overflow-tooltip="true"
                        align="left" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="物料名称" prop="material" min-width="150" :show-overflow-tooltip="true" align="left"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="供应商编号" prop="supplierCode" min-width="110" :show-overflow-tooltip="true"
                        align="left" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="供应商" prop="supplier" min-width="120" :show-overflow-tooltip="true" align="left"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="来料数量" min-width="100" :show-overflow-tooltip="true" prop="incomingQty"
                        align="left" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="批次号" min-width="120" :show-overflow-tooltip="true" prop="batchNumber"
                        align="left" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="采购订单号" prop="orderNo" min-width="120" :show-overflow-tooltip="true" align="left"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="检验员" prop="employeeName" align="left" min-width="90"
                        :show-overflow-tooltip="true" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="检验类型" prop="inspectTypeName" min-width="100" :show-overflow-tooltip="true"
                        align="left" sortable='custom'>
                    </el-table-column>
                    <el-table-column label="备注" prop="remark" align="left" min-width="120" :show-overflow-tooltip="true"
                        sortable='custom'>
                    </el-table-column>
                    <el-table-column label="操作" fixed="right" width="100">
                        <template slot-scope="scope">
                            <!--<el-button type="text" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>-->
                            <el-button type="text" v-if="scope.row.statusDesc == '未确认'"
                                @click="handleConfirm(scope.row)">确认</el-button>
                            <el-button type="text" @click="handleDel(scope.row.id)" class='SHZY-table-delBtn'>删除</el-button>
                        </template>
                    </el-table-column>
                </SHZY-table>
                <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize"
                    @pagination="initData" />
            </div>
        </div>
        <SHZY-Form v-if="formVisible" ref="SHZYForm" @refresh="refresh" />
        <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    </div>
</template>
<script>
import restAPI from '@/api/q1/business/inspection';
import Enumerable from 'linq'
import { GetSupplierAll } from '@/api/q1/basedata/supplier';
import { getMaterialAll } from '@/api/q1/basedata/material';
import SHZYForm from './Form'
import ExportBox from './ExportBox'
export default {
    components: { SHZYForm, ExportBox },
    data() {
        return {
            showAll: false,
            query: {
                status: 0,
                inspectionCode: undefined,
                materialCode: undefined,
                supplier: undefined,
                incomingQty: undefined,
                batchNumber: undefined,
                orderNo: undefined,
                remark: undefined,
            },
            list: [],
            listLoading: true,
            multipleSelection: [],
            total: 0,
            listQuery: {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            },
            formVisible: false,
            exportBoxVisible: false,
            columnList: [
                { prop: 'inspectionCode', label: '报检单号' },
                { prop: 'statusDesc', label: '状态' },
                { prop: 'materialCode', label: '物料编码' },
                { prop: 'material', label: '物料名称' },
                { prop: 'supplierCode', label: '供应商编号' },
                { prop: 'supplier', label: '供应商' },
                { prop: 'inspectTypeCode', label: '检验类型编号' },
                { prop: 'inspectTypeName', label: '检验类型' },
                { prop: 'employeeName', label: '检验员' },
                { prop: 'incomingQty', label: '来料数量' },
                { prop: 'batchNumber', label: '批次号' },
                { prop: 'orderNo', label: '采购订单号' },
                { prop: 'remark', label: '备注' },
            ],
            materialOptions: [],
            supplierOptions: [],
            statusOptions: [{ name: "未确认", code: 0 }, { name: "已提交", code: 1 }, { name: "已确认", code: 2 }]
        }
    },
    computed: {
        menuId() {
            return this.$route.meta.modelId || ''
        }
    },
    created() {
        this.initData()
        //this.getfullNameOptions();

        GetSupplierAll().then(res => {
            this.supplierOptions = res.data.list;
        });
        getMaterialAll().then(res => {
            this.materialOptions = res.data.list;
        });
    },
    methods: {
        //getfullNameOptions()
        //{
        //    getDictionaryDataSelector('448687444825473541').then(res =>
        //    {
        //        this.fullNameOptions = res.data.list
        //    });
        //},
        sortChange({ column, prop, order }) {
            this.listQuery.sort = order == 'ascending' ? 'asc' : 'desc'
            this.listQuery.sidx = !order ? '' : prop
            this.initData()
        },
        initData() {
            this.listLoading = true;
            let _query = {
                ...this.listQuery,
                ...this.query,
                menuId: this.menuId,
            };
            let query = {}
            for (let key in _query) {
                if (Array.isArray(_query[key])) {
                    query[key] = _query[key].join()
                } else {
                    query[key] = _query[key]
                }
            }

            //console.log(query)

            restAPI.getInspectionList(query).then(res => {
                //console.log(res.data.list);

                this.list = res.data.list
                this.total = res.data.pagination.total
                this.listLoading = false
            });
        },
        // 确认
        handleConfirm(row) {
            //console.log("C", row)
            restAPI.confirmInspection(row.inspectionCode)
                .then(res => {
                    if (res.data) {
                        this.$message.error(res.data);
                    }
                    this.search();
                })
                .catch(ex => {
                    console.error(ex);
                    this.$message.error(ex);
                });

        },
        // 删除
        handleDel(id) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                type: 'warning'
            }).then(() => {
                restAPI.deleteInspection(id).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.initData()
                        }
                    });
                })
            }).catch(() => {
            });
        },
        handleSelectionChange(val) {
            const res = val.map(item => item.id)
            this.multipleSelection = res;
        },
        // 批量确认
        handleBatchConfirm() {
            if (!this.multipleSelection.length) {
                this.$message({
                    type: 'error',
                    message: '请选择一条数据',
                    duration: 1500,
                });
                return;
            }
            const ids = Enumerable.from(this.multipleSelection);
            const codes = Enumerable.from(this.list).where(w => ids.contains(w.id)).select(s => s.inspectionCode).toArray()

            restAPI.batchConfirmInspections(codes).then(res => {
                if (res.data) {
                    this.$message.error(res.data);
                }
                this.search();
            }).catch(ex => {
                console.error(ex);
                this.$message.error(ex);
            });
        },
        //合批
        handleCombineBatch() {
            if (!this.multipleSelection.length) {
                this.$message({
                    type: 'error',
                    message: '请选择一条数据',
                    duration: 1500,
                });
                return;
            }
            const ids = Enumerable.from(this.multipleSelection);
            const codes = Enumerable.from(this.list).where(w => ids.contains(w.id)).select(s => s.inspectionCode).toArray()

            restAPI.combineBatchInspections(codes).then(res => {
                if (res.data) {
                    this.$message.error(res.data);
                }
                this.search();
            }).catch(ex => {
                console.error(ex);
                this.$message.error(ex);
            });
        },
        handleBatchRemoveDel() {
            if (!this.multipleSelection.length) {
                this.$message({
                    type: 'error',
                    message: '请选择一条数据',
                    duration: 1500,
                })
                return;
            }
            const ids = this.multipleSelection
            this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
                type: 'warning'
            }).then(() => {
                restAPI.batchDeleteInspection(ids).then(res => {
                    this.$message({
                        type: 'success',
                        message: res.msg,
                        onClose: () => {
                            this.search()
                        }
                    });
                })
            }).catch(() => { })
        },
        addOrUpdateHandle(id) {
            this.formVisible = true
            this.$nextTick(() => {
                this.$refs.SHZYForm.init(id)
            });
        },
        exportData() {
            this.exportBoxVisible = true
            this.$nextTick(() => {
                this.$refs.ExportBox.init(this.columnList)
            });
        },
        download(data) {
            let query = { ...data, ...this.listQuery, ...this.query, menuId: this.menuId }
            restAPI.exportData(query).then(res => {
                if (!res.data.url) return
                this.shzy.downloadFile(res.data.url)
                this.$refs.ExportBox.visible = false
                this.exportBoxVisible = false
            });
        },
        search() {
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        },
        refresh(isrRefresh) {
            this.formVisible = false
            if (isrRefresh) this.reset()
        },
        reset() {
            for (let key in this.query) {
                this.query[key] = undefined
            }
            this.listQuery = {
                currentPage: 1,
                pageSize: 20,
                sort: "desc",
                sidx: "",
            }
            this.initData()
        }
    }
}
</script>
